/* Non-Volatile Storage (NVS) Read and Write a Value - Example

   For other examples please check:
   https://github.com/espressif/esp-idf/tree/master/examples

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "esp_log.h"

#include "nvs_info_RW.h"

static const char *TAG = "nvs info R/W";

NVS_INFO_E StartEinstellen_config_flag = NVS_StartEinstellen_INFO_NULL;

/* 写入开机配置 */
NVS_INFO_E nvs_StartEinstellen_information_write(char *inBuf,uint8_t len)
{
    esp_err_t err;
    nvs_handle_t nvs_handle;
    err = nvs_open("setConfig", NVS_READWRITE, &nvs_handle);
    if (err != ESP_OK) {
        ESP_LOGE(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err));
        return err;
    }
    err = nvs_set_blob(nvs_handle,"sting_val",inBuf,len);
    if (err != ESP_OK) {
        ESP_LOGE(TAG,"NVS writt error (%s)!\n", esp_err_to_name(err));
    } 
    err = nvs_commit(nvs_handle);
    nvs_close(nvs_handle);
    
    if (err == ESP_OK) ESP_LOGI(TAG,"sting value write done!\n");
    else ESP_LOGE(TAG,"sting value write error (%s)!\n", esp_err_to_name(err));

    return err;
}
/* 读取开机配置 */
NVS_INFO_E nvs_StartEinstellen_information_read(char *outBuf,uint8_t len)
{
    esp_err_t err;
    nvs_handle_t nvs_handle;

    err = nvs_open("setConfig", NVS_READWRITE, &nvs_handle);
    if (err != ESP_OK) {
        ESP_LOGE(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err));
        return NVS_WIFI_INFO_ERROR;
    }

    err = nvs_get_blob(nvs_handle,"sting_val",outBuf,&len);
    nvs_close(nvs_handle);

    if (err == ESP_OK) ESP_LOGI(TAG,"has start einstellen config info");
    else ESP_LOGE(TAG,"sting value read error (%s)!\n", esp_err_to_name(err));

    return err;
}
